{% extends 'layout.html' %}

{% block content %}
    <div class="container">

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">新建任务表单</h3>
            </div>
            <div class="panel-body">

                <form id="formAdd">
                    <div class="clearfix">
                        {% for field in form %}
                            <div class="col-xs-6">
                                <div class="form-group" style="position: relative; margin-bottom: 20px">
                                    <label>{{ field.label }}</label>
                                    {{ field }}
                                    <span class="error-msg" style="color: red;position: absolute;"></span>
                                </div>
                            </div>
                        {% endfor %}
                        <div class="col-xs-12">
                            <button id="btnAdd" type="button" class="btn btn-primary">提 交</button>
                        </div>
                    </div>

                </form>

            </div>
        </div>

        <div class="panel panel-default">
            <!-- 面板标题 -->
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                任务列表
            </div>

            <!-- 面板表格部分 -->
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>级别</th>
                    <th>负责人</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for obj in queryset %}
                    <tr>
                        <th>{{ obj.id }}</th>
                        <td>{{ obj.title }}</td>
                        <td>{{ obj.get_level_display }}</td>
                        <td>{{ obj.user }}</td>
                        <td>
                            <a class="btn btn-primary btn-xs" href="#">编辑</a>
                            <a class="btn btn-danger btn-xs" href="#">删除</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <ul class="pagination">
            {{ page_string }}
        </ul>
    </div>

{% endblock %}

{% block js %}
    <script type="text/javascript">
        $(function () {
            //页面框架加载完成之后代码自动执行
            bindAddEvent();
        })


        function bindAddEvent() {
            $("#btnAdd").click(function () {
                $(".error-msg").empty();

                $.ajax({
                    url: '/task/add/',
                    type: "post",
                    data: $("#formAdd").serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            alert("添加成功");
                            //用JS实现页面刷新
                            location.reload();
                        } else {
                            {#循环输出错误信息#}
                            $.each(res.error, function (name, data) {
                                $("#id_" + name).next().text(data[0]);
                            })
                        }
                    }
                })
            })
        }

    </script>
{% endblock %}